using Microsoft.AspNetCore.Mvc;
using ZR.Model.ProdManage.Dto;
using ZR.Model.ProdManage;
using ZR.Service.ProdManage.IProdManageService;
using ZR.Admin.WebApi.Filters;
using MiniExcelLibs;

//创建时间：2025-05-18
namespace ZR.Admin.WebApi.Controllers.ProdManage
{
    /// <summary>
    /// 三级装柜
    /// </summary>
    [Verify]
    [Route("ProdManage/ProdThreePack")]
    public class ProdThreePackController : BaseController
    {
        /// <summary>
        /// 三级装柜接口
        /// </summary>
        private readonly IProdThreePackService _ProdThreePackService;

        public ProdThreePackController(IProdThreePackService ProdThreePackService)
        {
            _ProdThreePackService = ProdThreePackService;
        }

        /// <summary>
        /// 查询三级装柜列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("pageList")]
        [ActionPermissionFilter(Permission = "prod:threePack:list")]
        public IActionResult PageProdThreePack([FromQuery] ProdThreePackQueryDto parm)
        {
            var response = _ProdThreePackService.GetPageList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询三级装柜详情
        /// </summary>
        /// <param name="PackId"></param>
        /// <returns></returns>
        [HttpGet("{PackId}")]
        [ActionPermissionFilter(Permission = "prod:threePack:query")]
        public IActionResult GetProdThreePack(string PackId)
        {
            var response = _ProdThreePackService.GetInfo(PackId);
            
            var info = response.Adapt<ProdThreePackDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加三级装柜
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "prod:threePack:add")]
        [Log(Title = "三级装柜", BusinessType = BusinessType.INSERT)]
        public IActionResult AddProdThreePack([FromBody] ProdThreePackDto parm)
        {
            var modal = parm.Adapt<ProdThreePack>().ToCreate(HttpContext);

            var response = _ProdThreePackService.AddProdThreePack(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新三级装柜
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "prod:threePack:edit")]
        [Log(Title = "三级装柜", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateProdThreePack([FromBody] ProdThreePackDto parm)
        {
            var modal = parm.Adapt<ProdThreePack>().ToUpdate(HttpContext);
            var response = _ProdThreePackService.UpdateProdThreePack(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除三级装柜
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "prod:threePack:delete")]
        [Log(Title = "三级装柜", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteProdThreePack([FromBody] string[] ids)
        {
            return ToResponse(_ProdThreePackService.Delete(ids));
        }

        /// <summary>
        /// 导出三级装柜
        /// </summary>
        /// <returns></returns>
        [Log(Title = "三级装柜", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "prod:threePack:export")]
        public IActionResult Export([FromQuery] ProdThreePackQueryDto parm)
        {
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _ProdThreePackService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "三级装柜", "三级装柜");
            return ExportExcel(result.Item2, result.Item1);
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile"></param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "三级装柜导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false)]
        [ActionPermissionFilter(Permission = "prod:threePack:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile)
        {
            List<ProdThreePackDto> list = new();
            using (var stream = formFile.OpenReadStream())
            {
                list = stream.Query<ProdThreePackDto>(startCell: "A1").ToList();
            }

            return SUCCESS(_ProdThreePackService.ImportProdThreePack(list.Adapt<List<ProdThreePack>>()));
        }

        /// <summary>
        /// 三级装柜导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("importTemplate")]
        [Log(Title = "三级装柜模板", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult ImportTemplateExcel()
        {
            var result = DownloadImportTemplate(new List<ProdThreePackDto>() { }, "ProdThreePack");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}